import os
import sys

def main():
    # 读取"jp.txt"的内容并存入变量jp
    with open("jp.txt", "r", encoding="utf-8") as f:
        jp = f.read()

    # 读取"cn*.txt"的内容并存入变量cn
    with open("cn*.txt", "r", encoding="utf-8") as f:
        cn = f.read()

    # 读取文本A的内容并存入变量txt
    with open(sys.argv[1], "r", encoding="utf-8") as f:
        txt = f.read()

    # 确保cn和jp中的字符数量相同
    if len(cn) != len(jp):
        print("Error: cn*.txt和jp.txt的字符数量不一致！")
        return

    # 创建一个字典，将简体中文字符映射到对应的日文字符
    mapping = {cn[i]: jp[i] for i in range(len(cn))}

    # 将文本A中的简体中文字符按照映射关系替换为对应的日文汉字
    result = "".join(mapping.get(char, char) for char in txt)

    # 获取文本A的文件名（包含路径）
    source_file_path = sys.argv[1]

    # 提取出目录部分
    source_dir = os.path.dirname(source_file_path)

    # 生成新的文件名，以".new"后缀
    new_filename = os.path.join(source_dir, os.path.basename(source_file_path) + ".new")

    # 将转换后的结果写入新的文件
    with open(new_filename, "w", encoding="utf-8") as f:
        f.write(result)

    print("转换完成，结果已保存到", new_filename)

if __name__ == "__main__":
    main()

